package dao;

import entity.FilmInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tool.MyBatisUtil;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FilmInfoDaoImpl implements FilmInfoDao {
    SqlSessionFactoryBuilder builder = null;
    SqlSessionFactory factory = null;
    SqlSession sqlSession = null;
    //输入流读取配置文件
    Reader reader = null;
    public List<FilmInfo> getFilmInfo() {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //读取配置文件
            reader= Resources.getResourceAsReader("mybatis-config.xml");
            //构建builder
            builder = new SqlSessionFactoryBuilder();
            //构建factory
            factory = builder.build(reader);
            //构建sqlSession
            sqlSession = factory.openSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list = sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfo();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByName(String filmname) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //读取配置文件
            reader= Resources.getResourceAsReader("mybatis-config.xml");
            //构建builder
            builder = new SqlSessionFactoryBuilder();
            //构建factory
            factory = builder.build(reader);
            //构建sqlSession
            sqlSession = factory.openSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list = sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByName(filmname);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition(String filmname, Integer money) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition(filmname, money);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition2(Map<String, Object> map) {

        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition2(map);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition3(FilmInfo info) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition3(info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition4(FilmInfo info) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition4(info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition5(String choice) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition5(choice);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition6(Integer[] typeid) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition6(typeid);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByCondition7(List<Integer> typeid) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByCondition7(typeid);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoAndType() {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoAndType();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public int getFilmCount() {
        int count=0;
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).getFilmCount();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoAndTypeByPage(Integer pageIndex, Integer pageSize) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoAndTypeByPage(pageIndex,pageSize);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getFilmCount2(FilmInfo info) {
        int count=0;
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).getFilmCount2(info);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoAndTypeByPage2(Integer pageIndex, Integer pageSize,FilmInfo info) {
        List<FilmInfo> list = new ArrayList<>();
        try {
            //构建sqlSession
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfoAndTypeByPage2(pageIndex,pageSize,info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }
}
